import { ACTION_TYPES } from 'utils/constant';
import { Get, Post } from 'utils/handler';

export const getGroupMembers = (id) => dispatch => {
    dispatch({
        type: ACTION_TYPES.GROUP_MEMBERS_REQUEST
    })
    Get('/group/members', {
        data: {
            id
        }
    }).then(res => {
        if (res.reasonCode === 200) {
            dispatch({
                type: ACTION_TYPES.GROUP_MEMBERS_SUCCESS,
                members: res.members
            })
        } else {
            dispatch({
                type: ACTION_TYPES.GROUP_MEMBERS_FAIL
            })
        }
    }).catch(e => {
        dispatch({
            type: ACTION_TYPES.GROUP_MEMBERS_FAIL
        })
    })
}

export const quitGroup = (groupId, id) => dispatch => {
    return Post('/group/quit', {groupId, id}).then(res => {
        if (res.reasonCode === 200) {
            dispatch({
                type: ACTION_TYPES.GROUP_QUIT_SUCCESS,
                id
            })
            dispatch({
                type: ACTION_TYPES.UPDATE_LINKMANS_MESSAGE,
                id: groupId
            })
            return true;
        } else {
            return false;
        }
    }).catch(e => {
        return false;
    })
}